/**
 * Data Transport class used to post data to site
 */
package com.kudostar.android.kudostar;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;

import android.location.Location;
import android.util.Log;
import android.widget.EditText;

/**
 * @author Mike Luby
 * 
 */

public class DataTransport {

	// URL used to submit star
	final String URL_SUMBIT_STAR = "http://www.kudostar.com/test.php";
	// URL used for sync
	final String URL_SYNC = "http://www.google.com/";
	// URL used to check for update
	final String URL_UPDATE = "http://www.google.com/";
	//URL used to login
	final String URL_LOGIN = "http://www.google.com/";
	
	/**
	 * Construtor
	 */
	public DataTransport() {

	}

	/**
	 * Class used to post data to website
	 * 
	 * @param url Url to webservice
	 * @param data Hashmap of data to be included into post
	 * @param MediaURL Absolute path for media ( if needed )
	 * @return Data from site
	 */
	public String PostData( String postURL, HashMap data, String MediaURL ) {
		/**
		 * Send Data to server
		 */
		
		String lineEnd = "\r\n";
		String twoHyphens = "--";
		String boundary =  "*****";
		String responseFromServer = "";
		HttpURLConnection conn = null;
		try {
			URL url = new URL( postURL );
			conn = (HttpURLConnection) url.openConnection( );
			conn.setDoInput( true );
			conn.setDoOutput(  true  );
			conn.setUseCaches( false );
			conn.setRequestMethod( "POST" );
			conn.setRequestProperty( "Connection", "Keep-Alive" );
			conn.setRequestProperty( "User-Agent", "KudoStar/Android" );
			conn.setRequestProperty( "Content-Type", "multipart/form-data;boundary=" + boundary );
			DataOutputStream dos = new DataOutputStream( conn.getOutputStream( ) );
			if( MediaURL != null ) {
				FileInputStream fileInputStream = new FileInputStream( new File( MediaURL ) );
				dos.writeBytes(  twoHyphens + boundary + lineEnd );
				dos.writeBytes( "Content-Disposition: form-data; name=\"upload\"; filename=\"" + MediaURL + "\"" + lineEnd + lineEnd );
				int bytesAvailable = fileInputStream.available( );
				int bufferSize = Math.min( bytesAvailable, 1048576 );
				byte[ ] buffer = new byte[ bufferSize ];
				int bytesRead = fileInputStream.read( buffer, 0, bufferSize );
				while( bytesRead > 0 ) {
					dos.write(  buffer, 0, bufferSize );
					bytesAvailable = fileInputStream.available( );
					bufferSize = Math.min(  bytesAvailable, 1048576 );
					bytesRead = fileInputStream.read( buffer, 0, bufferSize );
				}
				dos.writeBytes( lineEnd );
				fileInputStream.close( );
			}
			Iterator iterator = data.keySet( ).iterator( );
			while ( iterator.hasNext( ) ) {
				Object key = iterator.next( );
				Object value = data.get( key );
				dos.writeBytes(twoHyphens + boundary + lineEnd);
				dos.writeBytes("Content-Disposition: form-data; name=\"" + (String) key + "\";" + lineEnd + lineEnd);
				dos.writeBytes(  value.toString( ) );
				dos.writeBytes(lineEnd);
			}
			dos.writeBytes( twoHyphens + boundary + twoHyphens + lineEnd );
			dos.flush( );
			dos.close( );
		} catch( MalformedURLException e ) {
			Log.e( "DataTransport", "MalformedURLException: " + e );
		} catch( IOException e ) {
			Log.e( "DataTransport", "IOException: " + e );
		}
		/**
		 * Read Response from server
		 */
		try {
			DataInputStream inStream = new DataInputStream( conn.getInputStream( ) );
			String str = "";
			while( ( str = inStream.readLine( ) ) != null ) {
				responseFromServer += str;
			}
			inStream.close( );
		} catch( IOException e ) {
			Log.e( "DataTransport", "Return from server IOException: " + e );
		}
		return responseFromServer;
	}

	/**
	 * SubmitStar data to web service
	 * 
	 * @param contact User's ( who is receiving star ) phone number
	 * @param orig Device Number
	 * @param value KudoStar value
	 * @param editable Description
	 * @param editable2 tags
	 * @param location GPS Location
	 * @param mediaLocation Absolute path to file
	 * @return passes data along data that was retrieved from PostData()
	 */
	public String SubmitStar( String contact, String orig, int value, EditText editable, EditText editable2, Location location, String mediaLocation ) {
		HashMap<String, Object> info = new HashMap<String, Object>( );
		info.put( "ident", contact );
		info.put( "orig", orig );
		info.put( "value", value );
		info.put( "description", editable.getText( ) );
		info.put( "tags", editable2.getText( ) );
		info.put( "location", location );
		return this.PostData( this.URL_SUMBIT_STAR, info, mediaLocation );
	}
	public String Login( String username, String password ) {
		HashMap<String, Object> info = new HashMap<String, Object>( );
		info.put( "username", username );
		info.put( "password", password );
		return this.PostData( this.URL_LOGIN, info, null );
	}
}